<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library.Http
 */
class Dmf_Http_Request
{
    
    /**
     * The request body
     * 
     * @var string 
     */
    public $body;
    
    /**
     * The request info
     * 
     * @var array
     */
    private $_info = array();
    
    /**
     * Constructor
     * 
     * @param string $body
     * @param array $info
     * @param string|null $error
     */
    public function __construct( $body, $info, $error )
    {
        $this->body = $body;
        $this->_info = $info;
        $this->_error = $error;
        
        $this->http_code = $this->_info['http_code'];
    }
    
    /**
     * Url decode the content first 
     */
    public function urlDecode()
    {
        $this->_content = urldecode( $this->_content );
    }
    
    /**
     * Decript the content before returning it
     * 
     * @param string $encryption 
     */
    public function getDecrypted( $encryption )
    {
        switch( $encryption )
        {
            case 'base64':
                return base64_decode( $this->body );
            break;
            default:
                throw new Exception( 'Encryption type: "' . $encryption . '" is not supported by the Request object.' );
            break;
        }
    }
    
    /**
     * Get json-parsed content
     * 
     * @return array 
     */
    public function getJson( $asArray = true )
    {
        return json_decode( $this->body, $asArray );
    }
    
    /**
     * Get unserialized content
     * 
     * @return mixed 
     */
    public function getPhp()
    {
        return unserialize( $this->body );
    }
    
    /**
     * Get xml object with content
     * 
     * @return SimpleXMLElement 
     */
    public function getXml()
    {
        return new SimpleXMLElement( $this->body );
    }
    
    /**
     * Get body as string
     * 
     * @return string 
     */
    public function getBody()
    {
        return $this->body;
    }
    
    /**
     * Save the content to a file
     * 
     * @param string $filename 
     */
    public function save( $filename )
    {
        return @file_put_contents( $filename, $this->getBody(), LOCK_EX );
    }
    
    /**
     * Get error message
     * 
     * @return string 
     */
    public function getError()
    {
        return $this->_error;
    }
    
    /**
     * Are there any errors?
     * 
     * @return boolean 
     */
    public function ok()
    {
        return ( $this->body === false ) ? false : true;
    }
    
    /**
     * Get cUrl info
     * 
     * @param string $index
     * @return mixed
     */
    public function getInfo( $index )
    {
        return ( isset( $this->_info[ $index ] ) ) ? $this->_info[ $index ] : null;
    }
}